package navigation.wzg.com.navigation.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import navigation.wzg.com.navigation.MainActivity;
import navigation.wzg.com.navigation.R;
import navigation.wzg.com.navigation.db.Diary;
import navigation.wzg.com.navigation.db.greendao.DiaryDao;
import navigation.wzg.com.navigation.vo.AFaApplication;

public class NewsActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private List<ImageView> list=new ArrayList<>();
    private LinearLayout linearLayout;
    private Button button;
    private DiaryDao dao;
    private int[] img={R.drawable.guide_1, R.drawable.guide_2,R.drawable.guide_3};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_news);
        dao = AFaApplication.getApplication().getDaoSession().getDiaryDao();
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        final String date = sDateFormat.format(new Date());
        viewPager= (ViewPager) findViewById(R.id.viewPager_main);
        linearLayout= (LinearLayout) findViewById(R.id.ll_points);
        button= (Button) findViewById(R.id.button_main);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Diary insertData = new Diary(null, date, "点滴记忆", "用美好记录每一天\n由系统生成\n长按列表栏可删除此条日记");
                dao.insert(insertData);
                startActivity(new Intent(NewsActivity.this,MainActivity.class));
                Toast.makeText(NewsActivity.this, "请登陆后使用", Toast.LENGTH_SHORT).show();
                finish();
                overridePendingTransition(R.anim.out, R.anim.up);
            }
        });
            initDate();
    }
    private void initDate() {
        ImageView imageView;
        View point;
        for (int i=0;i<img.length;i++){
            imageView=new ImageView(this);
            imageView.setBackgroundResource(img[i]);
            list.add(imageView);
            point=new View(this);
            point.setBackgroundResource(R.drawable.points);
            point.setEnabled(false);
            LinearLayout.LayoutParams linear=new LinearLayout.LayoutParams(10,10);
            linear.leftMargin=10;
            linearLayout.addView(point,linear);
        }
        linearLayout.getChildAt(0).setEnabled(true);
        viewPager.setAdapter(new MyAdapter());
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                for(int i=0;i<linearLayout.getChildCount();i++){
                    if(i==position) {
                        linearLayout.getChildAt(i).setEnabled(true);
                    }else {
                        linearLayout.getChildAt(i).setEnabled(false);
                    }
                }
                if(position==linearLayout.getChildCount()-1){
                    button.setVisibility(View.VISIBLE);
                }else {
                    button.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    class MyAdapter extends PagerAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView view=list.get(position);
            container.addView(view);
            return list.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(list.get(position));
        }
    }

    public void onBackPressed() {

    }

}
